//
//  STDSGrainEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSGrainEffect_h
#define STDSGrainEffect_h

#include "STDSBaseEffect.hpp"

/// 马赛克效果
class STDSGrainEffect: public STDSBaseEffect {
public:
    STDSGrainEffect() {
        mEffectType = STDSEffectTypeGrain;
    }
    ~STDSGrainEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        mGraint = *floatValue;
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mGrainProgram == nullptr) {
            mGrainProgram = mContext->getProgram(kShaderSRCGrain);
        }
        return mGrainProgram;
    }
    
    void draw() override {
        auto program = this->getEffectProgram();
        if (program == nullptr) {
            return;
        }
        program->use();
        if (mTexture != nullptr) {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, mTexture->getId());
            program->setUniform(program->getUniform("inputImageTexture"), 0);
        
        }
        program->setUniform(program->getUniform("grain"), mGraint);
        program->setUniform2f(program->getUniform("size"), mTexture->getWidth(), mTexture->getHeight());
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    float mGraint{1.0};
    shared_ptr<STDSShaderProgram> mGrainProgram{nullptr};
};
#endif /* STDSGrainEffect_h */
